package it.unibo.iss.domotica.consumeControlling;


import static org.junit.Assert.*;
import junit.framework.TestSuite;
import it.unibo.iss.domotica.consumptionControlling.ConsumptionControllerFacade;
import it.unibo.iss.domotica.testcommon.TestCommon;

import org.junit.Before;
import org.junit.Test;

public class ConsumptionControllerFacadeTest extends TestCommon {

	ConsumptionControllerFacade consumptionController;
	
	
	@Override
	@Before
	public void setUp() throws Exception {
		super.setUp();
		
		//*** Consume Controller e receiver ***//
		consumptionController = new ConsumptionControllerFacade(Double.valueOf(prop.getProperty("consumoComplMax")));
	}

	@Test
	public void testGetCurrentConsumption() {
		String edName1 = "edIM1";
		String edName2 = "edSB0";
		String edName3 = "edSA2";
		double consume1 = 5.2;
		double consume2 = 2.1;
		double consume3 = 9.5;
		double total = consume1 + consume2 + consume3;
		
		assertEquals(0, consumptionController.getCurrentConsumption(), 0);
		
		try {
			elettrodomestici.setConsumptionEd(edName1, consume1);
			elettrodomestici.setConsumptionEd(edName2, consume2);
			elettrodomestici.setConsumptionEd(edName3, consume3);
		}
		catch(Exception e) {
			e.printStackTrace();
		}
		
		
		consumptionController.updateConsumption();
		
		assertEquals(total, consumptionController.getCurrentConsumption(), 0);
	}

	@Test
	public void testThresholdExceeded() {
		String edName1 = "edIM1";
		String edName2 = "edSB0";
		String edName3 = "edSA2";
		double consume1 = 850.0;
		double consume2 = 456.5;
		double consume3 = 1900.0;
		
		assertFalse(consumptionController.thresholdExceeded());
		
		try {
			elettrodomestici.setConsumptionEd(edName1, consume1);
			elettrodomestici.setConsumptionEd(edName2, consume2);
			elettrodomestici.setConsumptionEd(edName3, consume3);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		consumptionController.updateConsumption();
		
		assertTrue(consumptionController.thresholdExceeded());
	}

//	@Test
//	public void testApproximateConsume() {
//		String edName1 = "edIM1";
//		String edName2 = "edSB0";
//		String edName3 = "edSA2";
//		double consumption1 = 5.2;
//		double consumption2 = 2.1;
//		double consumption3 = 9.5;
//		double consumption4 = 3.6;
//		double total = consumption1 + consumption2 + consumption3 + consumption4;
//		
//		try {
//			elettrodomestici.setConsumptionEd(edName1, consumption1);
//			elettrodomestici.setConsumptionEd(edName2, consumption2);
//			elettrodomestici.setConsumptionEd(edName3, consumption3);
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//		
//		consumptionController.updateConsumption();
//		
//		assertEquals(total, consumptionController.approximateConsumption(consumption4), 0);
//	}

	@Test
	public void testUpdateConsumption() {
		String edName1 = "edIM1";
		String edName2 = "edSB0";
		String edName3 = "edSA2";
		double consumption1 = 5.6;
		double consumption2 = 2.4;
		double consumption3 = 7.3;
		double total = consumption1 + consumption2 + consumption3;
		
		assertEquals(0, consumptionController.getCurrentConsumption(), 0);
		
		try {
			elettrodomestici.setConsumptionEd(edName1, consumption1);
			elettrodomestici.setConsumptionEd(edName2, consumption2);
			elettrodomestici.setConsumptionEd(edName3, consumption3);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		consumptionController.updateConsumption();
		
		assertEquals(total, consumptionController.getCurrentConsumption(), 0.01);
	}

	@Test
	public void testCanIPowerOnED() {
		String edName1 = "edIM1";
		String edName2 = "edSB0";
		String edName3 = "edSM2";
		double consumption1 = 745.0;
		double consumption2 = 378.0;
		double consumption3 = 834.0;	// consume1 + consume2 + consume3 = 1957 (threshold max = 3000 (properties file))
		double consumptionTrue = 550.0;
		double consumptionFalse = 1315.0;
		
		try {
			elettrodomestici.setConsumptionEd(edName1, consumption1);
			elettrodomestici.setConsumptionEd(edName2, consumption2);
			elettrodomestici.setConsumptionEd(edName3, consumption3);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		consumptionController.updateConsumption();
		
		assertTrue(consumptionController.canIPowerOnED(consumptionTrue));
		assertFalse(consumptionController.canIPowerOnED(consumptionFalse));
	}
	
//	@Test
//	public void testAttach() {
//		fail("Not yet implemented");
//	}
//
//	@Test
//	public void testDetach() {
//		fail("Not yet implemented");
//	}
//
//	@Test
//	public void testStart() {
//		fail("Not yet implemented");
//	}
}
